home *** CD-ROM | disk | FTP | other *** search
- Path: newsbf02.news.aol.com!not-for-mail
- From: smalherbe@aol.com (SMalherbe)
- Newsgroups: comp.lang.c++
- Subject: Overloading operator->
- Date: 12 Feb 1996 02:16:29 -0500
- Organization: America Online, Inc. (1-800-827-6364)
- Sender: root@newsbf02.news.aol.com
- Message-ID: <4fmpgd$rko@newsbf02.news.aol.com>
- Reply-To: smalherbe@aol.com (SMalherbe)
-
- I am attempting to implement a standard "indirection via a proxy"
- design pattern. I do this using the textbook overloading operator->
- approach. This works if the proxy (B in my example) is declared to
- be either automatic or static, but not on the heap. This is a
- significant limitation. The problem appears to be that I cannot
- do a "real" dereference and then use my overloaded operator without
- doing it explicitly (ie. "(*B)->"). This seems pretty ugly. Is there
- a way around this or am I missing something?
-
- class A {
- public:
- A () : x (0) {}
- int f() const {return x;}
- private:
- int x;
- };
-
- class B {
- public:
- B () {aPtr = new A;}
- A *operator->() {return aPtr;}
- private:
- A *aPtr;
- };
-
- main ()
- {
- B myB;
- B *myBPtr = new B;
-
- myB->f(); // <--- This is OK
- myBPtr->f(); // <--- This produces the following error:
-
- // proxy.cpp(23:12) : error EDC3079: "f" is not a member of "B".
-
- (*myBPtr)->f(); // <--- Is also OK
- }
-
- Thanks in advance for any advice,
-
- Stephen Malherbe
-